<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>图片轮播</title>
    <style>
        html,
        body,
        button,
        img,
        p {
            margin: 0;
            padding: 0;
        }
        
        button {
            cursor: pointer;
        }
        
        html,
        body {
            overflow: hidden;
        }
        
        #operationBar {
            margin: 30px auto;
            text-align: center;
        }
        
        #operationBar button {
            width: 200px;
            height: 30px;
            outline: none;
        }
        
        #operationBar button:last-child {
            margin-left: 20px;
        }
        
        #imgWrapper {
            position: absolute;
            left: 50%;
            top: 40%;
            transform: translate(-50%, -50%);
        }
        
        #prev {
            position: absolute;
            left: 7%;
            top: 50%;
            width: 30px;
            height: 30px;
            transform: translate(-50%, -50%);
        }
        
        #next {
            position: absolute;
            right: 7%;
            top: 50%;
            width: 30px;
            height: 30px;
            transform: translate(50%, -50%);
        }
        
        #imgWrapper img {
            width: 500px;
            height: 300px;
        }
        
        #imgWrapper p {
            position: absolute;
            top: 0;
            width: 100%;
            height: 30px;
            background-color: #000000;
            opacity: 0.7;
            font-size: 14px;
            line-height: 30px;
            color: #ffffff;
            text-align: center;
        }
    </style>
</head>

<body>
    <div id="operationBar">
        <button id="circle">循环切换</button>
        <button id="sequence">顺序切换</button>
    </div>
    <div id="imgWrapper">
        <p><span id="now">1</span>/4</p>
        <img id="content" src="./img/1.jpg" alt="">
        <button id="prev"><</button>
        <button id="next">></button>
    </div>
    <script>
        var playType = 0;
        var i = 0;
        var img = ['./img/1.jpg', './img/2.jpg', './img/3.jpg', './img/4.jpg']
        var content = document.getElementById('content');
        var now = document.getElementById('now');
        document.getElementById('circle').onclick = function() {
            playType = 0;
            alert('循环播放');
        }
        document.getElementById('sequence').onclick = function() {
            playType = 1;
            alert('顺序播放');
            i = Math.abs(i);
        }
        document.getElementById('prev').onclick = function() {
            if (i === 0) {
                if (playType === 0) {
                    i = 3;
                } else {
                    alert('已经是第一张了！');
                }
            } else {
                i--;
            }
            content.src = img[i];
            now.innerHTML = i + 1;
        }

        document.getElementById('next').onclick = function() {
            if (i === 3) {
                if (playType === 0) {
                    i = 0;
                } else {
                    alert('已经是最后一张了！');
                }
            } else {
                i++;
            }
            content.src = img[i];
            now.innerHTML = i + 1;
        }
    </script>
</body>

</html>